#include "crecipemodel.h"

#include <QSqlRecord>
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>
#include "crecipemodel.h"
#include "cconstdefinition.h"


const QString CRecipeModel::KRecipeRoleNamesMap[][2] = {
    {KRecipeId, KTableRecipe + KDot + KRecipeId},
//    {KRecipeCuisineId, KTableRecipe + KDot + KRecipeCuisineId},
    {KRecipeName, KTableRecipe + KDot + KRecipeName},
    {KRecipePicture, KTableRecipe + KDot + KRecipePicture},
    {KNullString, KNullString}
};

CRecipeModel::CRecipeModel(QObject *parent) :
    CModel(parent), m_cuisineId(-1), m_roleStart(-1), m_roleEnd(-1) {
    initRoles();
    refresh();
}

CRecipeModel::CRecipeModel(int cuisineId, QObject *parent) :
    CModel(parent), m_cuisineId(cuisineId), m_roleStart(-1), m_roleEnd(-1) {
    initRoles();
    refresh();
}

int CRecipeModel::rowCount(const QModelIndex& parent) const {
    int count = QSqlQueryModel::rowCount(parent);
    qDebug()<<"rowCount:"<<count;
    return count;
}

QVariant CRecipeModel::data(const QModelIndex& index, int role) const {
    QVariant value;
    if (role >= m_roleStart && role <= m_roleEnd) {
        int column = role - m_roleStart;
        QModelIndex roleIndex = this->index(index.row(), column);
        value = QSqlQueryModel::data(roleIndex);
    }
    else {
        qDebug()<<"wrong role:"<<role;
    }

    return value;
}

int CRecipeModel::cuisineId() {
    return m_cuisineId;
}

void CRecipeModel::setCuisineId(int cuisineId) {
    if (m_cuisineId != cuisineId) {
        beginResetModel();
        m_cuisineId = cuisineId;
        setQuery(queryString());
        emit cuisineIdChanged();
        endResetModel();
    }
}

void CRecipeModel::initRoles() {
    QHash<int, QByteArray> roles;
    int index = 0;

    while (KRecipeRoleNamesMap[index][0] != KNullString) {
        roles[index + KRecipeRoleStart] = KRecipeRoleNamesMap[index][0].toLocal8Bit();
        ++index;
    }
    m_roleStart = KRecipeRoleStart;
    m_roleEnd = index + KRecipeRoleStart - 1;
    setRoleNames(roles);
}

void CRecipeModel::refresh() {
    qDebug()<<"refresh";
    setQuery(queryString());
}

QString CRecipeModel::queryString() {
    QString queryString(KSelect);
    int i = 0;
    bool roleAdded = false;
    while (KRecipeRoleNamesMap[i][0] != KNullString) {
        // only add real columns
        if (KRecipeRoleNamesMap[i][1] != KNullString) {
            //add comma if role added and not the first one.
            if (i != 0 && roleAdded) {
                queryString += KComma;
            }
            queryString += KRecipeRoleNamesMap[i][1];
            roleAdded = true;
        }
        ++i;
    }
    queryString += KFrom + KTableRecipe + KJoin + KTableCuisineRecipe
            + KOn + KTableCuisineRecipe + KDot + KCuisineRecipeRecipeId
            + KEqual + KTableRecipe + KDot + KRecipeId;
    queryString += KWhere + KTableCuisineRecipe + KDot + KCuisineRecipeCuisineId + KEqual
            + QString::number(m_cuisineId);
    qDebug()<<"query string:"<<queryString;
    return queryString;
}
